<?php
/**
 * Created by PhpStorm.
 * User: chenyuz
 * Date: 2019/3/5
 * Time: 9:42
 */
namespace app\ops\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Login extends Controller
{
    public function index(Request $request) {
        if ($request->isPost()) {
            $userName = $request->post("username");
            $password = $request->post("password");
            if (empty($userName) || empty($password)) {
                $this->error("请填写账号和密码");
            }
            $userInfo = Db::name("admin_user")->field(["user_id as userId", "user_name as userName", "name", "user_position as userPosition", "password"])->where(["user_name"=> $userName])->find();

            if (empty($userInfo)) {
                $this->error("账号不存在,请重新输入");
            }

            if (md5("a@j@h#m$".$password) != $userInfo["password"]) {
                $this->error("密码不正确，请重新输入");
            }
           Session::set("userInfo", $userInfo);
            $this->redirect("/ops/Index/index");
        }
        $this->assign("title", "登录");
        return $this->fetch();
    }

    public function logout() {
        Session::set("userInfo", "");
        $this->redirect("/ops/Login/index");
    }
}
